<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */
namespace App\Http\Middleware;

use Closure;
use Route, URL, Auth, Gate;

class Role
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure                 $next
     *
     * @return mixed
     */
    public function handle( $request, Closure $next )
    {
        if ( Auth::user()->hasRole('超级管理员') ) return $next($request);//超级管理员就放行
        if ( Auth::user()->static == 2 ) {//判断账户是否被禁用
            if ( $request->ajax() && ( $request->getMethod() != 'GET' ) ) return data(NULL, '您的账户被禁用', 419);//ajax返回
            else abort(419);//返回异常页面
        }
        if ( Gate::denies(Route::currentRouteName()) ) {
            if ( $request->ajax() && ( $request->getMethod() != 'GET' ) ) return data(NULL, '您没有权限操作', 403);//ajax返回
            else abort(403);//返回异常页面
        }
        return $next($request);
    }
}
